import os
import random
import sys
import time

from PyQt5.QtCore import QPropertyAnimation, QRect, QUrl, QTimer, QStringListModel, QTime, Qt
from PyQt5.QtGui import QColor, QPainter, QPalette, QBrush, QPixmap, QStandardItemModel, QStandardItem, QIcon
from PyQt5.QtMultimedia import QMediaContent
from PyQt5.QtWidgets import QWidget, QApplication, QLineEdit, QMessageBox, QPushButton, QLabel, QLCDNumber, QFileDialog, \
    QGridLayout
from PyQt5 import QtWidgets, QtGui, QtCore

from untitled import Ui_Dialog_friut
from yc import yycc


class mainWindow(QWidget, Ui_Dialog_friut):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.ui_init()
        self.setWindowTitle('主界面')
        self.slot_init()
        self.model = QStandardItemModel()
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.show_image)
        self.current_index = 0

    def ui_init(self):
        pass

    def slot_init(self):
        pass
        self.pushButton_train.clicked.connect(self.train_click)
        self.pushButton_friut.clicked.connect(self.friut_click)
        self.pushButton_begin.clicked.connect(self.begin_click)

    def train_click(self):
        print('train_model1')

    def friut_click(self):
        print('friut')
        # 打开文件选择对话框
        file_dialog = QFileDialog()
        file_path, _ = file_dialog.getOpenFileName(self, '选择图片文件', '', '图片文件 (*.png *.jpg *.jpeg)')
        # print(file_path)
        if file_path:
            # 加载图片
            pixmap = QPixmap(file_path)
            # 创建一个 QLabel 用于显示图片
            label = QtWidgets.QLabel(self.gridLayoutWidget)
            label.setPixmap(pixmap)
            label.setScaledContents(True)  # 让图片适应标签大小
            # 获取 gridLayoutWidget 的布局，如果没有则创建一个
            if self.gridLayoutWidget.layout() is None:
                grid_layout = QtWidgets.QGridLayout(self.gridLayoutWidget)
            else:
                grid_layout = self.gridLayoutWidget.layout()
            # 将标签添加到网格布局中
            grid_layout.addWidget(label, 0, 0)
        cla = yycc(file_path)
        self.textEdit_group.clear()
        self.textEdit_group.append(str(cla))
        print(cla)

    def begin_click(self):
        self.current_index = 0
        print('begin')
        folder_path = QFileDialog.getExistingDirectory(self, '选择文件夹')
        self.file_paths = []
        if folder_path:
            # 使用 os.walk() 递归遍历文件夹
            for root, dirs, files in os.walk(folder_path):
                # root: 当前遍历的文件夹路径（字符串）
                # dirs: 当前文件夹下的子文件夹列表（字符串列表）
                # files: 当前文件夹下的文件列表（字符串列表）
                # 遍历当前文件夹中的所有文件
                for file in files:
                    # 将文件的完整路径（root + 文件名）添加到 self.file_paths 列表中
                    self.file_paths.append(os.path.join(root, file))
            print(self.file_paths)

            # 启动定时器，间隔 1000 毫秒（即 1 秒）
            self.show_image()
            self.timer.start(1000)

    def show_image(self):
        # layout = self.gridLayoutWidget.layout()
        # if layout:
        #     for i in reversed(range(layout.count())):
        #         item = layout.itemAt(i)
        #         widget = item.widget()
        #         if widget:
        #             widget.deleteLater()
        print('llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll')
        # 加载图片
        pixmap = QPixmap(self.file_paths[self.current_index])
        # 创建一个 QLabel 用于显示图片
        label = QtWidgets.QLabel(self.gridLayoutWidget)
        label.setPixmap(pixmap)
        label.setScaledContents(True)  # 让图片适应标签大小
        # 获取 gridLayoutWidget 的布局，如果没有则创建一个
        if self.gridLayoutWidget.layout() is None:
            grid_layout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        else:
            grid_layout = self.gridLayoutWidget.layout()
        # 将标签添加到网格布局中
        grid_layout.addWidget(label, 0, 0)

        cla = yycc(self.file_paths[self.current_index])
        self.textEdit_group.clear()
        self.textEdit_group.append(str(cla))
        print(cla)
        self.current_index = self.current_index + 1


if __name__ == '__main__':
    app = QApplication(sys.argv)
    login_window = mainWindow()
    login_window.show()
    sys.exit(app.exec_())
